package hos.ui.item;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;

import java.util.Map;

import hos.adapter.CommonArrayAdapter;
import hos.java.color.ColorConfig;
import hos.ui.R;
import hos.ui.spinner.SpinnerExtend;

/**
 * <p>Title: ItemTextLayout </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023/6/9 17:45
 */
public class ItemSpinnerLayout extends ItemTitleLayout {

    private SpinnerExtend spinner;

    public ItemSpinnerLayout(Context context) {
        this(context, null);
    }

    public ItemSpinnerLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ItemSpinnerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void initAttributeSet(Context context, AttributeSet attrs, int defStyleAttr) {
        @SuppressWarnings("resource") TypedArray typedArray =
                context.obtainStyledAttributes(attrs, R.styleable.ItemSpinnerLayout);
        try {
            final CharSequence[] entries = typedArray.getTextArray(R.styleable.ItemSpinnerLayout_android_entries);
            spinner = genSpinner();
            if (entries != null) {
                int spinnerColor = typedArray.getColor(
                        R.styleable.ItemSpinnerLayout_spinnerColor,
                        ColorConfig.transparent
                );
                //px
                int spinnerSize = typedArray.getDimensionPixelSize(
                        R.styleable.ItemSpinnerLayout_spinnerSize,
                        0
                );
                int spinnerDownColor = typedArray.getColor(
                        R.styleable.ItemSpinnerLayout_spinnerDownColor,
                        ColorConfig.transparent
                );
                //px
                int spinnerDownSize = typedArray.getDimensionPixelSize(
                        R.styleable.ItemSpinnerLayout_spinnerDownSize,
                        0
                );
                Drawable background = typedArray.getDrawable(
                        R.styleable.ItemSpinnerLayout_spinnerBackground
                );
                if (background != null) {
                    spinner.setBackground(background);
                }
                CommonArrayAdapter<CharSequence> adapter = new CommonArrayAdapter<>(context, android.R.layout.simple_spinner_item, entries);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                if (spinnerColor == ColorConfig.transparent) {
                    spinnerColor = -1;
                }
                if (spinnerDownColor == ColorConfig.transparent) {
                    spinnerDownColor = -1;
                }
                if (spinnerSize == 0) {
                    spinnerSize = -1;
                }
                if (spinnerDownSize == 0) {
                    spinnerDownSize = -1;
                }
                adapter.setText(spinnerColor, spinnerSize, spinnerDownColor, spinnerDownSize);
                spinner.setAdapter(adapter);
            }
            int orientation = getOrientation();
            if (orientation == HORIZONTAL) {
                int childGravity = typedArray.getInt(R.styleable.ItemSpinnerLayout_childGravity, Gravity.LEFT | Gravity.CENTER_VERTICAL);
                spinner.setGravity(childGravity);
                LayoutParams layoutParams = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
                layoutParams.weight = 1;
                spinner.setLayoutParams(layoutParams);
            } else {
                int childGravity = typedArray.getInt(R.styleable.ItemSpinnerLayout_childGravity, Gravity.LEFT | Gravity.TOP);
                spinner.setGravity(childGravity);
                spinner.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
            }
            int childMinHeight = typedArray.getDimensionPixelOffset(R.styleable.ItemRadioLayout_childMinHeight, dp2px(48));
            spinner.setMinimumHeight(childMinHeight);
            addView(spinner);
        } finally {
            typedArray.recycle();
        }
    }

    @Override
    public void setContent(Map<String, Object> map, String key) {
        setKey(key);
        if (spinner == null) {
            return;
        }
        if (!map.containsKey(key)) {
            return;
        }
        Object value = map.get(key);
        if (value == null) {
            return;
        }
        SpinnerAdapter adapter = spinner.getAdapter();
        if (adapter instanceof ArrayAdapter) {
            try {
                int position = ((ArrayAdapter<Object>) adapter).getPosition(value);
                spinner.setSelection(position);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void setContent(CharSequence content) {
        if (spinner == null) {
            return;
        }
        SpinnerAdapter adapter = spinner.getAdapter();
        if (adapter instanceof ArrayAdapter) {
            try {
                int position = ((ArrayAdapter<CharSequence>) adapter).getPosition(content);
                spinner.setSelection(position);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public String getText() {
        if (spinner == null) {
            return "";
        }
        Object selectedItem = spinner.getSelectedItem();
        if (selectedItem == null) {
            return "";
        }
        return selectedItem.toString();
    }

    @Override
    public void clear() {
        if (spinner == null) {
            return;
        }
        spinner.setSelection(0);
    }

    protected SpinnerExtend genSpinner() {
        return new SpinnerExtend(getContext());
    }

    public SpinnerExtend getSpinner() {
        return spinner;
    }

    public SpinnerAdapter getAdapter() {
        if (spinner == null) {
            return null;
        }
        return spinner.getAdapter();
    }

    public void setOnSelectedClickListener(SpinnerExtend.OnItemClickListener listener) {
        if (spinner == null) {
            return;
        }
        spinner.setOnSelectedClickListener(listener);
    }

    /**
     * @return The data corresponding to the currently selected item, or
     * null if there is nothing selected.
     */
    public Object getSelectedItem() {
        if (spinner == null) {
            return null;
        }
        return spinner.getSelectedItem();
    }

    public int getSelectedItemPosition() {
        if (spinner == null) {
            return -1;
        }
        return spinner.getSelectedItemPosition();
    }

    public void setSelection(int position) {
        if (spinner == null) {
            return;
        }
        spinner.setSelection(position);
    }

    public void setAdapter(SpinnerAdapter adapter) {
        if (spinner == null) {
            return;
        }
        spinner.setAdapter(adapter);
    }

    public void setContentEnabled(boolean enabled) {
        if (spinner != null) {
            spinner.setEnabled(enabled);
        }
    }

    @Override
    public void setEnabled(boolean enabled) {
        if (spinner != null) {
            spinner.setEnabled(enabled);
        }
        super.setEnabled(enabled);
    }

    public void setClickableEnabled(boolean clickable) {
        if (spinner != null) {
            spinner.setClickable(clickable);
        }
    }

    @Override
    public void setClickable(boolean clickable) {
        if (spinner != null) {
            spinner.setClickable(clickable);
        }
        super.setClickable(clickable);
    }
}
